﻿using BJoin.Application.AppException;
using BJoin.Application.BObject;
using BJoin.Application.WebApi.Extensions;
using BJoin.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace BJoin.MicroSVC.Sample.Webapi.Controllers.File
{
    public partial class FileController
    {

        [HttpPost("BodyTypes/JsonStringBody")]
        public IActionResult JsonStringBody([FromBody] string content)
        {
            return Ok(content);
        }
        [HttpPost("BodyTypes/ReadStringDataManual")]
        public async Task<IActionResult> ReadStringDataManualAsync()
        {
            return Ok(await Request.GetRawBodyStringAsync());
        }
        /// <summary>
        /// 根据编码和名称查询记录总数(Expression)
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="name">名称</param>
        /// <returns>总数</returns>
        /// <response code="200">返回数目</response>
        /// <response code="400">参数不正确...</response>
        /// <response code="500">内部错误...</response>
        [HttpGet("count-expression")]
        [ProducesResponseType(typeof(int), 200)]
        [ProducesResponseType(typeof(string), 400)]
        [ProducesResponseType(typeof(string), 500)]
        public async Task<IActionResult> CountAsync(string name, string code = "code1")
        {
            var count = await _service.HelloExpression(code, name);
            return Ok(count);
        }
        /// <summary>
        /// 文件上传接口
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("upload-file")]
        [DisableRequestSizeLimit]
        public async Task<IActionResult> UploadAsync(IFormFile file)
        {
            if (file == null)
                return BadRequest(string.Format(EnumHelper.GetEnumDescription(EnumMessage.NotNull), nameof(file)));
            bool result = await _service.UploadAsync(new List<IFormFile> { file });
            return Ok(result);
        }
        /// <summary>
        /// 文件下载接口
        /// </summary>
        /// <returns></returns>
        [HttpGet("download-file/{id}")]
        //[AllowAnonymous]
        public async Task<IActionResult> DownLoad(long id)
        {
            string pathDir = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "wwwroot");
            if (!System.IO.Directory.Exists(pathDir))
                System.IO.Directory.CreateDirectory(pathDir);
            var fileUrl = await _service.DownloadAsync(id);
            string fullName = Path.Combine(pathDir, fileUrl);
            /* zip file
             var zipFile = $"{zipFolder}.zip";
            var zipFileName = $"{folderName}.zip";
            ZipFile.CreateFromDirectory(zipFolder, zipFile);
             */
            if (!System.IO.File.Exists(fullName))
                throw new HttpStatusCodeException((int)System.Net.HttpStatusCode.NotFound,
                    string.Format(EnumHelper.GetEnumDescription<EnumMessage>(EnumMessage.NotFound),fileUrl));
            var file = new FileInfo(fullName);
            return File(file.OpenRead(), "application/octet-stream", Path.GetFileName(fullName));
        }
    }
}